//公共方法
Designer.addFunction("open", function (definition, fn) {
    if (definition == "") {
        return;
    }
    if (typeof definition == "string") {
        eval("definition = " + definition);
    }
    $(".shape_box").remove();
    Model.define.elements = {};
    Model.persistence.elements = {};
    Model.define.page = definition.page;
    Model.persistence.page = Utils.copy(definition.page);
    Model.comments = definition.comments;
    if (definition.theme) {
        Model.define.theme = definition.theme;
        Model.persistence.theme = Utils.copy(definition.theme)
    }
    if (localRuntime == false) {
        Designer.initialize.initCanvas();
    }
    var shapes = definition.elements;
    var shapeCount = 0;
    if( JSON.stringify(shapes) == '{}'){
        Designer.painter.renderShape('', "", true);
    }
    for (var shapeId in shapes) {
        var shape = shapes[shapeId];
        Schema.initShapeFunctions(shape);
        Designer.painter.renderShape(shape, "", true);
        Model.add(shape, false);
        shapeCount++
    }
    if (shapeCount == 0) {
        Model.build();
    }
    if(typeof fn == "function"){
        fn(Model);
    }
});
//全选
Designer.addFunction("selectAll", function () {
    var a = Model.define.elements;
    var b = [];
    for (var c in a) {
        b.push(c);
    }
    Utils.selectShape(b);
});
//样式
Designer.addFunction("setLineStyle", function (f, fid) {
    var e = Utils.getNotLockSelected();
    var _this=Model.define.elements[fid];
    if (e.length == 0) {
        if(fid){
            e.push(_this);
        }else{
            return
        }
    }
    var d = Utils.getFamilyShapes(e);
    e = e.concat(d);
    for (var c = 0; c < e.length; c++) {
        var a = e[c];
        a.lineStyle = Utils.copy(a.lineStyle);
        var ft=e[c];
        ft.fontStyle=Utils.copy(ft.fontStyle);
        var co=e[c];
        co.componentsObj=Utils.copy(co.componentsObj);
        if (typeof f.lineWidth != "undefined") {
            a.lineStyle.lineWidth = f.lineWidth;
            Utils.unselect();
             if(a.name == "transverseline"){
                Model.define.elements[a.id].props.h = f.lineWidth;
                $("#" + a.id + " .shape_canvas").css("height", f.lineWidth);
            }
            if(a.name == "longstring"){
                Model.define.elements[a.id].props.w = f.lineWidth;
                $("#" + a.id + " .shape_canvas").css("width", f.lineWidth);
            }
        }
        if (typeof f.lineColor != "undefined") {
            if(a.name == "transverseline" || a.name == 'longstring'){
                a.lineStyle.lineColor = f.lineColor;
                $("#" + a.id + " .shape_canvas").css("background-color", "rgb(" + f.lineColor + ")");
            }
            if(ft.name == "text" ){
                ft.fontStyle.color = f.lineColor;
                $("#" + ft.id + " .shape_canvas").css("color", "rgb(" + f.lineColor + ")");
            }
            if( ft.name == "myPlace" ){
                ft.fontStyle.color = f.lineColor;
                $("#" + ft.id + " .shape_canvas").css("color", "rgb(" + f.lineColor + ")");
            }
            if(ft.name == "arrow" ){
                ft.fontStyle.color = f.lineColor;
                $("#" + ft.id + " .shape_canvas").css("color", "rgb(" + f.lineColor + ")");
            }
            if( ft.name == "obstacle"){
                ft.fontStyle.color = f.lineColor;
                $("#" + ft.id + " .shape_canvas").css("color", "rgb(" + f.lineColor + ")");
            }
        }
        if (typeof f.size != "undefined") {
             ft.fontStyle.size = f.size;
             if(ft.name == "text"){
                $("#" + ft.id + " .shape_canvas").css("font-size", f.size);
            }
        }
        if (typeof f.fontFamily != "undefined") {
            ft.fontStyle.fontFamily = f.fontFamily;
            if(ft.name == "text"){
                $("#" + ft.id + " .shape_canvas").css("font-family", f.fontFamily);
            }
        }
        if (typeof f.imgsrc != "undefined") {
            ft.componentsObj.imgurl = f.imgsrc;
            if(ft.name == "uploadPhoto"){
                $("#" + ft.id + " .shape_canvas img").attr("src", f.imgsrc);
            }
        }
        if(typeof f.comsvgHtml != "undefined"){
            co.componentsObj.comsvg = f.comsvg;
            co.componentsObj.comsvgHtml = f.comsvgHtml;
            if(co.name == "obstacle" || co.name == "arrow" || co.name == "myPlace" || co.name == "mapTime"){
                $("#" + co.id + " .shape_canvas").html(f.comsvgHtml);
            }
            if(co.name == "mapTime"){
                co.props.w=parseFloat($("#" + co.id + " .shape_canvas .mapTimeDiv").css("width"));
            }
        }
        if(typeof f.computerNum != "undefined"){
            co.componentsObj.computerNum = f.computerNum;
            if(co.name == "addComputer" ){
                $("#" + co.id + " .shape_canvas .cpNum").val(f.computerNum);
                $("#"+ co.id+" .shape_canvas").attr("data-val",f.computerNum);
            }
        }
        if(typeof f.computerBgColor != "undefined"){
            co.componentsObj.computerBgColor = f.computerBgColor;
            co.componentsObj.computerBgColor2 = f.computerBgColor2;
            co.componentsObj.state = f.state;
            if(co.name == "addComputer" ){
                var $this=$("#" + co.id + " .shape_canvas");
                $this.css("background-color",f.computerBgColor);
                $this.children('input,span').css("background-color",f.computerBgColor2);
                $this.attr("data-state",f.state);
            }
        }
        if(typeof f.textAer != "undefined"){
            co.componentsObj.textAer=f.textAer;
        }
        Utils.selectShape(e[0].id);
    }
    Model.updateMulti(e);
    for (var c = 0; c < e.length; c++) {
        var a = e[c];
        Designer.painter.renderShape(a);
        for (var b = 0; b < g.length; b++) {
            if (!Utils.isSelected(g[b])) {
                var h = Model.getShapeById(g[b]);
                Designer.painter.renderShape(h);
            }
        }
    }
});
//设置层级
Designer.addFunction("layerShapes", function (m) {
    var h = Utils.getSelected();
    if (h.length == 0 || !m) {
        return
    }
    h.sort(function c(r, i) {
        return r.props.zindex - i.props.zindex
    });
    var b;
    if (m == "front") {
        b = Model.maxZIndex;
        for (var j = 0; j < h.length; j++) {
            var l = h[j];
            b += 1;
            l.props.zindex = b;
            Model.maxZIndex++;
            $("#"+l.id).css("z-index", b);
        }
    } else {
        if (m == "forward") {
            var d = null;
            var q = null;
            for (var j = 0; j < h.length; j++) {
                var l = h[j];
                d = n(l);
                if (d != null) {
                    q = l.props.zindex;
                    break
                }
            }
            if (d == null) {
                return
            }
            var k = d.props.zindex;
            var f = n(d);
            var p = k + 1;
            if (f != null) {
                p = k + (f.props.zindex - k) / 2
            }
            var g = p - q;
            for (var j = 0; j < h.length; j++) {
                var l = h[j];
                l.props.zindex += g
            }
        } else {
            if (m == "back") {
                b = Model.orderList[0].zindex;

                for (var j = h.length - 1; j >= 0; j--) {
                    var l = h[j];
                    b -= 1;
                    if(b < 2){
                        var tempzindex = l.props.zindex - 2 < 1 ? 1 : l.props.zindex - 2;
                        for(var propsk in Model.define.elements){
                            if(propsk === l.id) b = 2;
                            if(Model.define.elements[propsk].name === "download"){
                                Model.define.elements[propsk].props.zindex = 1;
                            }else{
                                Model.define.elements[propsk].props.zindex += tempzindex;
                            }
                            $("#"+ propsk).css("z-index", Model.define.elements[propsk].props.zindex);
                        }
                    }
                    l.props.zindex = b
                    $("#"+l.id).css("z-index", b);
                }

            } else {
                if (m == "backward") {
                    var a = null;
                    var q = null;
                    for (var j = 0; j < h.length; j++) {
                        var l = h[j];
                        a = e(l);
                        if (a != null) {
                            q = l.props.zindex;
                            break
                        }
                    }
                    if (a == null) {
                        return
                    }
                    var k = a.props.zindex;
                    var o = e(a);
                    var p = k - 1;
                    if (o != null) {
                        p = k - (k - o.props.zindex) / 2
                    }
                    var g = p - q;
                    for (var j = 0; j < h.length; j++) {
                        var l = h[j];
                        l.props.zindex += g
                    }
                }
            }
        }
    }
    Model.updateMulti(h);
    function n(s) {
        var v = Utils.getShapeBox(s);
        for (var u = 0; u < Model.orderList.length; u++) {
            var i = Model.orderList[u];
            if (i.zindex <= s.props.zindex || Utils.isSelected(i.id)) {
                continue
            }
            var t = Model.getShapeById(i.id);
            var r = Utils.getShapeBox(t);
            if (Utils.rectCross(v, r)) {
                return t
            }
        }
        return null
    }

    function e(s) {
        var v = Utils.getShapeBox(s);
        for (var u = Model.orderList.length - 1; u >= 0; u--) {
            var i = Model.orderList[u];
            if (i.zindex >= s.props.zindex || Utils.isSelected(i.id)) {
                continue
            }
            var t = Model.getShapeById(i.id);
            var r = Utils.getShapeBox(t);
            if (Utils.rectCross(v, r)) {
                return t
            }
        }
        return null
    }
});
//锁定
Designer.addFunction("lockShapes", function () {
    var b = Utils.getSelectedIds();
    if (b.length == 0) {
        return;
    }
    var d = [];
    for (var c = 0; c < b.length; c++) {
        var a = Model.getShapeById(b[c]);
        a.locked = true;
        $('#'+a.id).css("overflow","inherit");
        d.push(a);
    }
    Utils.unselect();
    Utils.selectShape(b);
    Model.updateMulti(d, true);
});
//解锁
Designer.addFunction("unlockShapes", function (id) {
    var b = id || Utils.getSelectedLockedIds();
    if (b.length == 0) {
        return
    }
    var d = [];
    for (var c = 0; c < b.length; c++) {
        var a = Model.getShapeById(b[c]);
        a.locked = false;
        $('#'+a.id).css("overflow","hidden");
        d.push(a)
    }
    var e = Utils.getSelectedIds();
    Utils.unselect();
    Utils.selectShape(e);
    Model.updateMulti(d);
});
//画布设置
Designer.addFunction("setPageStyle", function (a, b) {
    if(b){
        Model.updatePage(a, "", b);
    }else{
        Model.updatePage(a);
    }
});
//放大
Designer.addFunction("zoomIn", function () {
    var a = Designer.config.scale;
    var b = a + 0.1;
    Designer.setZoomScale(b);
});
//缩小
Designer.addFunction("zoomOut", function () {
    var a = Designer.config.scale;
    var b = a - 0.1;
    Designer.setZoomScale(b);
});
//设置缩放
Designer.addFunction("setZoomScale", function (e) {
    if (e < 0.25) {
        e = 0.25
    }
    if (e > 4) {
        e = 4
    }
    Designer.config.scale = e;
    Designer.initialize.initCanvas();
    for (var d in Model.define.elements) {
        var b = Model.define.elements[d];
        Designer.painter.renderShape(b);
    }
    var a = Utils.getSelectedIds();
    var c = Utils.getSelectedLockedIds();
    Utils.mergeArray(a, c);
    Utils.unselect();
    Utils.selectShape(a);
});
//添加监听函数
Designer.events.addEventListener("initialized", function () {
    if (localRuntime) {
        Designer.open(definition);
    }
});
Designer.events.addEventListener("selectChanged", function () {
    UI.update();
});
Designer.events.addEventListener("undoStackChanged", function (a) {
    if (a == 0) {
        $("#bar_list_edit").children("li[ac=undo]").menuitem("disable");
        $("#bar_undo").button("disable")
    } else {
        $("#bar_list_edit").children("li[ac=undo]").menuitem("enable");
        $("#bar_undo").button("enable")
    }
});
Designer.events.addEventListener("redoStackChanged", function (a) {
    if (a == 0) {
        $("#bar_list_edit").children("li[ac=redo]").menuitem("disable");
        $("#bar_redo").button("disable");
    } else {
        $("#bar_list_edit").children("li[ac=redo]").menuitem("enable");
        $("#bar_redo").button("enable");
    }
});
Designer.events.addEventListener("beforeRemove", function (c) {
    var n = {};
    for (var g = 0; g < c.length; g++) {
        var k = c[g];
        n[k.id] = k
    }
    c = [];
    for (var b in n) {
        c.push(n[b])
    }
    return c
});
Designer.events.addEventListener("removed", function (b) {
    var c = b.shapes;
    var m = b.range;
    var s = b.changedIds;
    var g = [];
    var r = [];
    return g
});